<?php
// +----------------------------------------------------------------------
// | ADM Software [ 我宁可 为做过的事而后悔，也不想 因为没尝试而后悔。 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2014 扬州安蝶梦电子商务有限公司 All rights reserved.
// +----------------------------------------------------------------------
// | Author: 曹梦龙 <138888611@qq.com> <http://www.andiemeng.com>
// +----------------------------------------------------------------------
namespace Org\Util;


class AEObject {
    private $_classname;
    private $_dataArray = array();
    public  $objectId; 
    private $AeHttp;

     /*
     * 构造函数
     */
    function __construct($databasename='',$tablename = '') { //初始化对象，将初始化值放在括号内
        $this->_classname = $tablename;
        $this->AeHttp = new \Org\Util\AEHttp($databasename);
    }

    /*
     * 设置属性
     */
    public function set($name, $value) {
        $this->_dataArray[$name] = $value;
    } 

    /*
     * 读取属性
     */
    public function get($name) {
        return $this->_dataArray[$name];
    }
    
    /*
     * 返回对象属性 数组类型
     */
    public function getArray(){
        $dataArray= $this->_dataArray;
        $dataArray['objectId']=$this->objectId;
        return $dataArray;
    }
    
    /**
     * 新增加或者修改
     */
     public function save() {
        //新增加
        if (is_null($this->objectId)) {
            $this->_dataArray['createAt']=NOW_TIME;             
            $result = $this->AeHttp->create($this->_classname, $this->_dataArray);
            $this->objectId = $result->data->insertId; 
            return (array) $result;
        }
        //修改
        if (!is_null($this->objectId)) {
            $this->_dataArray['updateAt']=NOW_TIME;
            $result = $this->AeHttp->update($this->_classname, "id=".$this->objectId, $this->_dataArray); 
            return (array) $result;
        }
    }
    
    //删除对象
    public function destroy(){
        $result = $this->AeHttp->remove($this->_classname,$this->objectId); 
        return (array) $result->body;
    }
    
    //设置对象
    public function _QuerySet($body_rwa){
        $this->objectId=$body_rwa['id']; 
        unset($body_rwa['objectId']); 
        $this->_dataArray=$body_rwa;
        return $this;
    }
}
